package org.example.Util;

import org.example.Pojo.User;

import javax.swing.*;
import java.io.*;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class FileUtil {
    //文件配置
    private String fileAddress;
    private File file=null;
    private static File folder=new File("F:/数据结构课设/sudoku/用户信息");//文件夹信息
    private static File[] files;//文件数组

    //文件的读写
    private static BufferedWriter bw=null;
    private static BufferedReader br=null;

    //注册之后对文件进行创建，文件名字为username,password.txt
    public void CreateFile(User user) throws IOException {
        fileAddress="F:/数据结构课设/sudoku/用户信息/"+user.getName()+"."+user.getPassword()+".txt";//文件名地址设定
        file=new File(fileAddress);
        file.createNewFile();
    }
    //登录或者注册的时候对文件中的名字和密码获取到哈希表中
    public Map<String ,String> Find(){
        files=folder.listFiles();//获取数组
        Map<String ,String> map=new HashMap<>();//暂存哈希表
        for(File file:files){
            String[] user=file.getName().split("\\.");//user[0]==username,user[1]==password,user[2]=txt
            map.put(user[0],user[1]);
        }
        return map;
    }
    //对图片文件进行读取，返回一维数组
    public Vector<File> Readimgs(){
        File imgAddress=new File("F:/数据结构课设/sudoku/图/图/完整图");
        File[] imgs=imgAddress.listFiles();//提取到照片
        //提取到动态数组中
        Vector<File> res = new Vector<>();
        for(File img:imgs){
            res.add(img);
        }
        return res;
    }

    //获取文件夹中的所有切片
    public JLabel[][] ReadsectionImg(String fileingex,int length){
        File sectionAddress=new File("F:/数据结构课设/sudoku/图/图/"+fileingex);//读取文件夹
        File[] setions=sectionAddress.listFiles();//传入一维数组中
        JLabel[][] jLabels=new JLabel[length][length];
        //将图片添加到二维数组中去
        for(int i=0;i<length;i++){
            for(int j=0;j<length;j++){
                jLabels[j][i]=new JLabel();
                if(i!=length-1||j!=length-1)//限制了最后一张图片
                    jLabels[j][i].setIcon(new ImageIcon(setions[i*length+j].getAbsolutePath()));
            }
        }
        return jLabels;
    }

    //获取到完整照片
    public JLabel ReadCompeleteImg(String fileingex){
        File CompleteImgAddress=new File("F:/数据结构课设/sudoku/图/图/"+fileingex);
        File[] Imgs=CompleteImgAddress.listFiles();
        JLabel ComleteImg=new JLabel(new ImageIcon(Imgs[Imgs.length-1].getAbsolutePath()));
        return ComleteImg;
    }



    //获取所有用户成绩
    public static Vector<User>  ReadScore() throws IOException {
        File[] users=folder.listFiles();//转换为文件数组
        Vector<User>userVector=new Vector<>();
        for(File user:users){
            br = new BufferedReader(new FileReader(user.getAbsolutePath()));
            String firstline="";
            if((firstline= br.readLine())!=null)
            {
                String[] str = firstline.split(",");//分割获取数据
                User player=new User(str[0],str[1],Integer.parseInt(str[2]), LocalTime.parse(str[3]));
                userVector.add(player);
                System.out.println("读取到的数据为"+firstline);
            }
        }
        return userVector;
    }

    //对用户成绩进行撰写
    public static void WriteScore(User player) throws IOException {
        try {
            bw=new BufferedWriter(new FileWriter("F:/数据结构课设/sudoku/用户信息/"+player.getName()+"."+player.getPassword()+".txt",true));
            System.out.println("写入数据为"+player.getName()+","+player.getPassword()+","+player.getScore()+","+player.getLocalTime());
            bw.write(player.getName()+","+player.getPassword()+","+player.getScore()+","+player.getLocalTime());
            bw.newLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bw.close();
        }
    }

    //读取本用户所有成绩
    public static Vector<User> ReadplayScore(User player) throws IOException {
        br=new BufferedReader(new FileReader("F:/数据结构课设/sudoku/用户信息/"+player.getName()+"."+player.getPassword()+".txt"));//文件地址
        Vector<User>userVector=new Vector<>();
        String line="";
        StringBuilder content=new StringBuilder();
        while((line=br.readLine())!=null) {
            String[] str = line.split(",");//分割获取数据
            userVector.add(new User(str[0],str[1],Integer.parseInt(str[2]), LocalTime.parse(str[3])));
            System.out.println("读取到的数据为"+line);
        }
        return userVector;
    }
    //删除文件内容并且重新编写
    public static void DeleteandWrite(User player,Vector<User> players) throws IOException {
        try {
            bw=new BufferedWriter(new FileWriter("F:/数据结构课设/sudoku/用户信息/"+player.getName()+"."+player.getPassword()+".txt",false));
            bw.write("");
            System.out.println("文件内容已删除");
            for(User tem:players){
                bw=new BufferedWriter(new FileWriter("F:/数据结构课设/sudoku/用户信息/"+tem.getName()+"."+tem.getPassword()+".txt",true));
                System.out.println("写入数据为"+player.getName()+","+player.getPassword()+","+player.getScore()+","+player.getLocalTime());
                bw.write(tem.getName()+","+tem.getPassword()+","+tem.getScore()+","+tem.getLocalTime());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bw.close();
        }
    }
}


